/*
 * ===========================================================================
 *
 *      Filename: frame.c
 *   Description: 
 *        Author: Ye wenhao, wenhaoye@126.com
 *  Organization: 
 *
 * ===========================================================================
 */
#include "frame.h"

int frame_init(frame_t *self, frame_t *next, uint8_t id, uint32_t size)
{
	self->next = next;
	self->sync = FRAME_SYNC;
	self->id = id;
	self->len = 0;
	self->size = size;
	if (self->size > sizeof(self->data))
		return buffer_init(&self->buf, 2 * FRAME_DEF_SIZE);	

	return 0;
}

void frame_term(frame_t *self)
{
	self->next = NULL;
	if (self->size > FRAME_DEF_SIZE)
		buffer_term(&self->buf);
	self->size = 0;
	self->len = 0;
}

void frame_reset(frame_t *self)
{
	self->next = NULL;
	if (self->size > FRAME_DEF_SIZE)
		buffer_reset(&self->buf);
	self->len = 0;
}

int frame_add(frame_t *self, void *data, size_t len)
{
	int ret = 0;
	if (self->size > FRAME_DEF_SIZE) {
		ret = buffer_add(&self->buf, data, len);
	} else {
		memcpy(self->data + self->len, data, len);
	}

	if (ret)
		return -1;
	self->len += len;
	return 0;
}

